package com.wz.boot.function.naowan;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wz.boot.annotation.Function;
import com.wz.boot.annotation.Handle;
import com.wz.boot.constant.Constant;
import com.wz.boot.function.defaultchat.DefaultChat;
import com.wz.boot.utils.HttpUtils;
import love.forte.simbot.api.message.events.PrivateMsg;
import love.forte.simbot.api.sender.Sender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Function(
        level = 1,
        priority = 7,
        name = "脑筋急转弯",
        describe = "脑筋急转弯",
        touch = "脑筋急转弯",
        claName = "NaoWan"
)
@Component
public class NaoWan {
    final Logger logger = LoggerFactory.getLogger(DefaultChat.class);
    final String requestUrl = "http://api.tianapi.com/txapi/naowan/index";
    final String requestKey = "0daf4bf4ee9d40aca3ede135e8f51ffc";
    final String responseNum = "1";
    List<String[]> naowanList = new ArrayList<>();

    @Autowired
    private Constant constant;

    Map<Long,OneCon> statusMap = new HashMap<>();

    @Handle
    public void aiChat(PrivateMsg privateMsg, Sender sender) {
        Long accountCode = privateMsg.getAccountInfo().getAccountCodeNumber();
        refreshNaowan(privateMsg,sender);
        String[] strings = naowanList.get(new Random(System.currentTimeMillis()).nextInt(naowanList.size()));
        String reply = "";
        if(!statusMap.containsKey(accountCode)){
            OneCon oneCon = new OneCon(true,strings[0], strings[1]);
            sender.sendPrivateMsg(privateMsg,oneCon.getProblem());
            // true待发送resp   false待发送req
            statusMap.put(accountCode,oneCon);
        }else{
            OneCon oneCon = statusMap.get(accountCode);
            if(oneCon.isTag()){
                reply = oneCon.getResponse();
                sender.sendPrivateMsg(privateMsg,reply);
                oneCon.setTag(false);//下次是题目
                String[] newString = naowanList.get(new Random(System.currentTimeMillis()).nextInt(naowanList.size()));
                statusMap.put(accountCode,new OneCon(false,newString[0], newString[1]));
            }else{
                OneCon newOneCon = statusMap.get(accountCode);
                reply = newOneCon.getProblem();
                sender.sendPrivateMsg(privateMsg,reply);
                // true待发送resp   false待发送req
                oneCon.setTag(true);//下次是答案
            }
        }

        logger.info("QQ:{}发来了:{}    机器人回复了:{}",privateMsg.getAccountInfo().getAccountCode(),privateMsg.getMsgContent().getMsg(), reply);

    }

    private void refreshNaowan(PrivateMsg privateMsg, Sender sender){
        Map<String, String> params = new HashMap<>(2);
        params.put("key", requestKey);
        params.put("num", responseNum);
        boolean requestFalseTag = false;
        String web = HttpUtils.get(requestUrl, params).getResponse();
        int i = 0;
        JSONObject jsonObject = JSONObject.parseObject(web);
        int code = jsonObject.getInteger("code");
        switch (code) {
            case 200: {
                JSONArray newslist = jsonObject.getJSONArray("newslist");
                for (int j = 0; j < newslist.size(); j++) {
                    JSONObject naowan = newslist.getJSONObject(j);
                    naowanList.add(new String[]{naowan.getString("quest"),naowan.getString("result")});
                }
                logger.info("脑筋急转弯缓冲池:"+newslist.size());
                break;
            }
            default:{
                if(i++ < 20)
                    refreshNaowan(privateMsg,sender);
                else
                    sender.sendPrivateMsg(privateMsg, "出错啦！");
                    sender.sendPrivateMsg(constant.getAdminQQ(), "天行api接口出错,"+requestUrl);
            }
        }
    }

    class OneCon{
        private boolean tag;
        private String problem;
        private String response;


        public OneCon(boolean tag, String problem, String response) {
            this.tag = tag;
            this.problem = problem;
            this.response = response;
        }

        public boolean isTag() {
            return tag;
        }

        public void setTag(boolean tag) {
            this.tag = tag;
        }

        public String getProblem() {
            return problem;
        }

        public void setProblem(String problem) {
            this.problem = problem;
        }

        public String getResponse() {
            return response;
        }

        public void setResponse(String response) {
            this.response = response;
        }
    }
}
